<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/utils.tld" prefix="utils" %> 
<%@page import="java.util.GregorianCalendar" %>
<%@page import="java.util.ArrayList"%>
<%@page import="fr.unice.CD.calendar.Event"%>
<%@page import="fr.unice.CD.calendar.LoadXML"%>
<%@page import="fr.unice.CD.calendar.Semaine" %>
	<%
	//	on initialise les var de dates
	String months[] = { "janv.", "fevr.", "mars", "avr.", "mai",
			"juin", "juil.", "aout.", "sept.", "oct.", "nov.", "dec.", };
	String jours[] = { "Dimanche", "Lundi", "Mardi", "Mercredi",
			"Jeudi", "Vendredi", "Samedi" };
	
	GregorianCalendar aujourdhui = null;
	int position, jour, newposition, mois, annee;
	String dateparam = null;
	
	//recupere la date passer en URL
	String param = request.getParameter("date");
	if (param != null) {
		position = param.indexOf("\\");
		jour = Integer.parseInt(param.substring(0, position));
		newposition = param.indexOf("\\", position + 1);
		mois = Integer.parseInt(param.substring(position + 1,
				newposition));
		annee = Integer.parseInt(param.substring(newposition + 1));

		//initialise un nouveau calendrier en fonction de la date passer en URL
		aujourdhui = new GregorianCalendar(annee, mois - 1, jour);
		
		//initialise les var de la semaine
		Semaine.initVarOfTheWeek(aujourdhui);
		
	}
	else{
		//initialise un  nouveau calendrier
		aujourdhui = new GregorianCalendar();
		jour = aujourdhui.get(GregorianCalendar.DATE);
		mois = aujourdhui.get(GregorianCalendar.MONTH);
		annee = aujourdhui.get(GregorianCalendar.YEAR);
				
		//initialise les var de la semaine
		Semaine.initVarOfTheWeek(aujourdhui);
	}
	%>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<SCRIPT language="JavaScript" src="script.js"></SCRIPT>
		<jsp:useBean id="event" class="fr.unice.CD.calendar.BeanEvent" scope="application" />
		<%
		if(event.getLesEvent().size()==0){
			String path = this.getServletContext().getRealPath(
					request.getContextPath());
			LoadXML.loadXML(path, event);
		}
		%>
		<link href="style.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript">
		function dessineEvents()
		{
			<%
			int dayOfWeekparam = -1;
			String lundi = Semaine.day_of_first_day_of_week +"/"+Semaine.month_of_first_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			String mardi = Semaine.day_of_second_day_of_week +"/"+Semaine.month_of_second_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			String mercredi = Semaine.day_of_third_day_of_week +"/"+Semaine.month_of_third_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			String jeudi = Semaine.day_of_fourth_day_of_week +"/"+Semaine.month_of_fourth_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			String vendredi = Semaine.day_of_fifth_day_of_week +"/"+Semaine.month_of_fifth_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			String samedi = Semaine.day_of_sixth_day_of_week +"/"+Semaine.month_of_sixth_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			String dimanche = Semaine.day_of_seventh_day_of_week +"/"+Semaine.month_of_seventh_day_of_week+"/"+Semaine.year_of_first_day_of_week;
			
			//on affiche seulement les evenements dont la date = date du jour
			ArrayList<Event> listeEvents=event.getLesEvent();
			Event evenement;
			for (int i=0;i<listeEvents.size();i++){
				evenement=listeEvents.get(i);
				String dateEvent= evenement.getDateDebut();
				
				
				if(dateEvent.equals(lundi) || 
						dateEvent.equals(mardi) || dateEvent.equals(mercredi) || 
						dateEvent.equals(jeudi) || dateEvent.equals(vendredi) || 
						dateEvent.equals(samedi) || dateEvent.equals(dimanche)){
				String nomEvent = evenement.getNom();
				String heureDebut = evenement.getHeureDebut();
				String duree = evenement.getDuree();
				
				
				int pos = evenement.getDateDebut().indexOf("/");
				int tempjour = Integer.parseInt(evenement.getDateDebut().substring(0,pos));
				int newpos = evenement.getDateDebut().indexOf("/",pos+1);
				int tempmois = Integer.parseInt(evenement.getDateDebut().substring(pos+1, newpos));
				int tempannee = Integer.parseInt(evenement.getDateDebut().substring(newpos+1));
				GregorianCalendar tempCalend = new GregorianCalendar(tempannee, tempmois-1, tempjour);
				dayOfWeekparam = tempCalend.get(GregorianCalendar.DAY_OF_WEEK);
				String date =  tempjour+"/"+(tempmois-1)+"/"+tempannee ;
				
				%>
				var dayOfWeekparam = "<%=dayOfWeekparam%>" ;
				var date = "<%=date%>" ;
				var idEvent = "<%=i%>" ; 
				var heureDebut = "<%=heureDebut%>" ;
				var nomEvent = "<%=nomEvent%>" ;
				var duree = "<%=duree%>" ;
				ajouterEvenementprSemaine(nomEvent, idEvent, heureDebut, duree, dayOfWeekparam, date);
				<%
				}//fin IF
			} //fin FOR 
			%>
		}
		</script>
	</head>
	
	
	<body onMouseMove="detectMouse(event);" onload="dessineEvents()">
	<h2>Mon Agenda</h2>
	<div  id="granularite">	
		<ul>
		<li><a href="jour.jsp">Jour</a></li>
		<li><a href="semaine.jsp">Semaine</a></li>
		<li><a href="mois.jsp">Mois</a></li>
		<li><a href="planning.jsp">Mon planning</a></li>
		</ul>
	</div>
	
	<div id="colonnegauche">
		<a href="ajouter.jsp">Créer un événement</a><br/>
		<div id="divcalendar"><utils:monCal/></div>
	</div>
	
	<div id="colonnedroite">
	<% 
	GregorianCalendar semainePrecendente = (GregorianCalendar)aujourdhui.clone();
	semainePrecendente.add(GregorianCalendar.DATE,-7);
	GregorianCalendar semaineSuivante = (GregorianCalendar)aujourdhui.clone();
	semaineSuivante.add(GregorianCalendar.DATE,7);
	%> 
	<a href="semaine.jsp?date=<%=semainePrecendente.get(GregorianCalendar.DATE)
								+"\\"+(semainePrecendente.get(GregorianCalendar.MONTH)+1)
								+"\\"+semainePrecendente.get(GregorianCalendar.YEAR) %>">
		<input type="button" value=" < " class="buttonImg"></a>
	<a href="semaine.jsp?date=<%=semaineSuivante.get(GregorianCalendar.DATE)
								+"\\"+(semaineSuivante.get(GregorianCalendar.MONTH)+1)
								+"\\"+semaineSuivante.get(GregorianCalendar.YEAR) %>">
		<input type="button" value=" > " class="buttonImg"></a>
	<a href="semaine.jsp">
	<input type="button" value="Aujourd'hui" class="buttonImg"></a> &nbsp;&nbsp; 
	<%
	
	//affiche la date au format  "4 – 10 mai 2008" ou "29 juin 2008 – 5 juil. 2008" 
	if(Semaine.month_of_first_day_of_week != Semaine.month_of_seventh_day_of_week){
		out.print("<b>"+Semaine.day_of_first_day_of_week + " "+ months[Semaine.month_of_first_day_of_week-1] + " " + Semaine.year_of_first_day_of_week);
		out.print(" - ");
		out.print(Semaine.day_of_seventh_day_of_week + " "+ months[Semaine.month_of_seventh_day_of_week-1]+ " "+Semaine.year_of_seventh_day_of_week+"</b>");
	}
	else{
		out.print("<b>"+Semaine.day_of_first_day_of_week +" - "+Semaine.day_of_seventh_day_of_week+" "+ months[Semaine.month_of_first_day_of_week-1]+" "+Semaine.year_of_first_day_of_week+"</b>");
	}

	%>
		<table id="agendaHead">
		<thead>
				<tr>
					<th width="52px">
					&nbsp;
					</th>
					<th border="1"  align="center">
					Lun.
					<%
					out.print(Semaine.day_of_first_day_of_week +"/"+Semaine.month_of_first_day_of_week);%>
					</th>
					<th border="1"  align="center">
					Mar.
					<%out.print(Semaine.day_of_second_day_of_week +"/"+Semaine.month_of_second_day_of_week);%>
					</th>
					<th border="1"  align="center">
					Mer.
					<%out.print(Semaine.day_of_third_day_of_week +"/"+Semaine.month_of_third_day_of_week);%>
					</th>
					<th border="1"  align="center">
					Jeu.
					<%out.print(Semaine.day_of_fourth_day_of_week +"/"+Semaine.month_of_fourth_day_of_week);%>
					</th>
					<th border="1"  align="center">
					Ven.
					<%out.print(Semaine.day_of_fifth_day_of_week +"/"+Semaine.month_of_fifth_day_of_week);%>
					</th>
					<th border="1"  align="center">
					Sam.
					<%out.print(Semaine.day_of_sixth_day_of_week +"/"+Semaine.month_of_sixth_day_of_week);%>
					</th>
					<th border="1"  align="center">
					Dim.
					<%out.print(Semaine.day_of_seventh_day_of_week +"/"+Semaine.month_of_seventh_day_of_week);%>
					</th>
				</tr>
		</thead>
		</table>
	
		<div id="overflowAgenda">
			<div id="evenements">
			</div>
			<table id="agenda">
				<tbody>	
					<% 
					int tdid= 1;
					for(int i=0; i<=23; i++){		
						out.println("<tr>");
						out.println("<th width=\"7%\" rowspan=\"2\" valign=\"top\">"+i+":00</th>");
						out.println("<td id='td"+tdid+"' onclick=\"visibilitedivAjout(2, '"+Semaine.day_of_first_day_of_week +"/"+(Semaine.month_of_first_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":00', "+tdid+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid + 1)+"' onclick=\"visibilitedivAjout(3, '"+Semaine.day_of_second_day_of_week +"/"+(Semaine.month_of_second_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"','"+i+":00', "+(tdid+1)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid + 2)+"' onclick=\"visibilitedivAjout(4, '"+Semaine.day_of_third_day_of_week +"/"+(Semaine.month_of_third_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":00', "+(tdid+2)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid + 3)+"' onclick=\"visibilitedivAjout(5, '"+Semaine.day_of_fourth_day_of_week +"/"+(Semaine.month_of_fourth_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":00', "+(tdid+3)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid + 4)+"' onclick=\"visibilitedivAjout(6, '"+Semaine.day_of_fifth_day_of_week +"/"+(Semaine.month_of_fifth_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":00', "+(tdid+4)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid + 5)+"' onclick=\"visibilitedivAjout(7, '"+Semaine.day_of_sixth_day_of_week +"/"+(Semaine.month_of_sixth_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":00', "+(tdid+5)+");\">&nbsp;</td>");
						out.println("<td width=\"13%\" id='td"+(tdid + 6)+"' onclick=\"visibilitedivAjout(1, '"+Semaine.day_of_seventh_day_of_week +"/"+(Semaine.month_of_seventh_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":00', "+(tdid+6)+");\">&nbsp;</td>");
						out.println("</tr>");
						tdid += 10;
						out.println("<tr>");
						out.println("<td id='td"+(tdid)+"' onclick=\"visibilitedivAjout(2, '"+Semaine.day_of_first_day_of_week +"/"+(Semaine.month_of_first_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+tdid+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid+1)+"' onclick=\"visibilitedivAjout(3, '"+Semaine.day_of_second_day_of_week +"/"+(Semaine.month_of_second_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+(tdid+1)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid+2)+"' onclick=\"visibilitedivAjout(4, '"+Semaine.day_of_third_day_of_week +"/"+(Semaine.month_of_third_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+(tdid+2)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid+3)+"' onclick=\"visibilitedivAjout(5, '"+Semaine.day_of_fourth_day_of_week +"/"+(Semaine.month_of_fourth_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+(tdid+3)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid+4)+"' onclick=\"visibilitedivAjout(6, '"+Semaine.day_of_fifth_day_of_week +"/"+(Semaine.month_of_fifth_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+(tdid+4)+");\">&nbsp;</td>");
						out.println("<td id='td"+(tdid+5)+"' onclick=\"visibilitedivAjout(7, '"+Semaine.day_of_sixth_day_of_week +"/"+(Semaine.month_of_sixth_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+(tdid+5)+");\">&nbsp;</td>");
						out.println("<td width=\"13%\" id='td"+(tdid+6)+"' onclick=\"visibilitedivAjout(1, '"+Semaine.day_of_seventh_day_of_week +"/"+(Semaine.month_of_seventh_day_of_week-1)+"/"+Semaine.year_of_first_day_of_week+"', '"+i+":30', "+(tdid+6)+");\">&nbsp;</td>");
						out.println("</tr>");	
						tdid += 10;
					}
					%>
			</tbody>
		</table>	
		</div>
	
	<div id="divAjout" style="display: none;">
		<div id="heure_clique"></div><br />
		<form method='get' action='ajouter.jsp'>Objet: <input type="text"
		id="nameEvent" name='nom'><br />
		<input type='hidden' id="date" name='date' value=''>
		<input type='hidden' id="heure" name='heure' value=''> 
		<input type='hidden' id="minutes" name='minutes' value=''> 
		par exemple, projet d'application web<br /><br />
		<input type="submit" value="Crée un événement"></form>
	</div>
	
	<div id="divModif" style="display: none;"><b>
		<div id="nameEventSelect">nom Event</div>
		<div id='heureClique'></div>
		<form name="supprForm" id="supprForm" method='get' action='Suppression'>
		<input type='hidden' id="idEvent" name='id' value=''> [<a
			href="javascript: submitform('supprForm')">supprimer</a>]<br />
		</form>
		<hr>
		<form name="modifForm" id="modifForm" method='get' action='modifier.jsp'>
		<input type='hidden' id="idEvent2" name='id' value=''> <a
			href="javascript: submitform('modifForm')"> modifier les details de
		l'evenement >></a></form>
	</div>
	
</div><!-- //col de droite -->
	
	</body>
</html>